import unittest
from util.read_csv import CSVUtil
from ddt import ddt, data, unpack
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException

# 读取数据文件
filePath = "..\\testdata\\数据_ECShop_后台登录.csv"
u = CSVUtil(filePath)
d = u.get_list_data()
print(d)


@ddt
class MyBgLoginTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
        self.driver.implicitly_wait(15)

    def tearDown(self) -> None:
        sleep(2)
        self.driver.quit()

    # def is_alert_present(self):
    #     flag=True
    #     try:
    #         self.driver.switch_to.alert
    #     except NoAlertPresentException:
    #         flag=False
    #     return flag

    def is_alert_present(self):
        try:
            self.driver.switch_to.alert
        except NoAlertPresentException:
            return False
        return True

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(how, what)
        except NoSuchElementException:
            return False
        return True

    def close_alert_and_get_its_text(self):
        a = self.driver.switch_to.alert
        t = a.text
        a.accept()
        return t

    @data(*d)
    @unpack
    def test_bg_login(self, dataid, caseid, username, password, captcha, expected):
        print("数据编号,用例编号,用户名,密码,验证码,预期结果编号")
        print(dataid, caseid, username, password, captcha, expected)
        # 打开后台登录页
        self.driver.get("http://127.0.0.1/upload/admin/index.php")
        # 输入用户名、密码、验证码
        self.driver.find_element(By.NAME, "username").send_keys(username)
        self.driver.find_element(By.NAME, "password").send_keys(password)
        self.driver.find_element(By.NAME, "captcha").send_keys(captcha)
        # 点击“进入管理中心”
        self.driver.find_element(By.XPATH, "//input[@value='进入管理中心']").click()
        # 判断预期结果编号等于不同值，就做不同的检查
        if expected == "1":
            print("第一种检查")
            # 检查退出所属的header-frame元素出现在主网页里
            # flag11=True
            # try:
            #     self.driver.find_element(By.ID,"header-frame")
            # except NoSuchElementException:
            #     flag11=False # 代表这个frame没有找到
            flag11 = self.is_element_present(By.ID, "header-frame")
            self.assertTrue(flag11)
            # 切换到header-frame
            self.driver.switch_to.frame("header-frame")
            # 检查“退出”这个元素出现在这个子网页里
            # flag12=True
            # try:
            #     self.driver.find_element(By.LINK_TEXT,"退出")
            # except NoSuchElementException:
            #     flag12=False
            flag12 = self.is_element_present(By.LINK_TEXT, "退出")
            self.assertTrue(flag12)
            # 点击“退出”
            self.driver.find_element(By.LINK_TEXT, "退出").click()
        elif expected == "2":
            print("第二种检查")
            # 检查信息框出现（弹出来）
            # flag21=True
            # try:
            #     self.driver.switch_to.alert
            # except NoAlertPresentException:
            #     flag21=False #代表弹出框没有出现
            flag21 = self.is_alert_present()
            self.assertTrue(flag21)
            # 切换到信息框里
            # a2=self.driver.switch_to.alert
            # # 获取信息框里的文本
            # t2=a2.text
            t2 = self.close_alert_and_get_its_text()
            # 检查该文本等于“- 管理员用户名不能为空!”
            self.assertEqual("- 管理员用户名不能为空!\n", t2)
            # # 点击“确定”
            # a2.accept()
            # 检查信息框消失
            # flag22=True
            # try:
            #     self.driver.switch_to.alert
            # except NoAlertPresentException:
            #     flag22=False
            flag22 = self.is_alert_present()
            self.assertFalse(flag22)
        elif expected == "3":
            print("第三种检查")
            # 检查网页跳转到系统信息页：检查标题以“系统信息”结尾
            t31 = self.driver.title
            b31 = t31.endswith("系统信息")
            self.assertTrue(b31)
            # 检查网页里的信息内容是“您输入的帐号信息不正确。”
            ps3 = self.driver.page_source
            self.assertIn("您输入的帐号信息不正确。", ps3)  # 账号-帐号
            # 等待5秒
            sleep(5)
            # 检查跳转回登录页：检查网页标题等于“ECSHOP 管理中心”
            t32 = self.driver.title
            self.assertEqual("ECSHOP 管理中心", t32)
            # 检查密码文本框为空
            v3 = self.driver.find_element(By.NAME, "password").get_attribute("value")
            self.assertEqual("", v3)
        elif expected == "4":
            print("第四种检查")
            # 检查弹出框出现
            # flag41=True
            # try:
            #     self.driver.switch_to.alert
            # except NoAlertPresentException:
            #     flag41=False
            flag41 = self.is_alert_present()
            self.assertTrue(flag41)
            # # 切换到弹出框
            # a4=self.driver.switch_to.alert
            # # 获得弹出框里的文本
            # t4=a4.text
            t4 = self.close_alert_and_get_its_text()
            # 检查文本是“- 您没有输入验证码!”
            self.assertEqual("- 您没有输入验证码!\n", t4)
            # # 点击“确定”
            # a4.accept()
            # 检查弹出框消失
            # flag42=True
            # try:
            #     self.driver.switch_to.alert
            # except NoAlertPresentException:
            #     flag42=False
            flag42 = self.is_alert_present()
            self.assertFalse(flag42)
        elif expected == "5":
            print("第五种检查")
            # 检查弹出框出现
            # flag51=True
            # try:
            #     self.driver.switch_to.alert
            # except NoAlertPresentException:
            #     flag51=False
            flag51 = self.is_alert_present()
            self.assertTrue(flag51)
            # # 切换到弹出框
            # a5=self.driver.switch_to.alert
            # # 获得弹出框里的文本
            # t5=a5.text
            t5 = self.close_alert_and_get_its_text()
            # 检查文本是“- 管理员用户名不能为空!\n- 您没有输入验证码!\n”
            self.assertEqual("- 管理员用户名不能为空!\n- 您没有输入验证码!\n", t5)
            # # 点击“确定”
            # a5.accept()
            # 检查弹出框消失
            # flag52=True
            # try:
            #     self.driver.switch_to.alert
            # except NoAlertPresentException:
            #     flag52=False
            flag52 = self.is_alert_present()
            self.assertFalse(flag52)
        elif expected == "6":
            print("第六种检查")
            # 检查跳转到系统信息页：检查标题以“系统信息”结尾
            t61 = self.driver.title
            b61 = t61.endswith("系统信息")
            self.assertTrue(b61)
            # 检查信息内容是“您输入的验证码不正确。”
            ps6 = self.driver.page_source
            self.assertIn("您输入的验证码不正确。", ps6)
            # 点击“返回上一页”
            # print(ps6)
            self.driver.find_element(By.LINK_TEXT, "返回上一页").click()
            # 检查跳转回登录页：检查标题以“管理中心”结尾
            t62 = self.driver.title
            b62 = t62.endswith("管理中心")
            self.assertTrue(b62)
            # 检查密码文本框为空
            v6 = self.driver.find_element(By.NAME, "password").get_attribute("value")
            self.assertEqual("", v6)


if __name__ == '__main__':
    unittest.main()
